package com.njtoyo.taxi.admin.service.business.driver.impl;

import cn.hutool.core.date.DateUtil;
import com.njtoyo.taxi.admin.mapper.master.entity.DriverCustomMessage;
import com.njtoyo.taxi.admin.mapper.master.entity.DriverFlowLog;
import com.njtoyo.taxi.admin.mapper.master.entity.DriverReward;
import com.njtoyo.taxi.admin.mapper.master.entity.DriverRewardLog;
import com.njtoyo.taxi.admin.mapper.master.enums.*;
import com.njtoyo.taxi.admin.mapper.master.mapper.DriverCustomMessageMapper;
import com.njtoyo.taxi.admin.mapper.master.mapper.DriverFlowLogMapper;
import com.njtoyo.taxi.admin.mapper.master.mapper.DriverRewardLogMapper;
import com.njtoyo.taxi.admin.mapper.master.mapper.DriverRewardMapper;
import com.njtoyo.taxi.admin.rest.wrapper.business.driver.DriverRewardAddOrUpdateWrapper;
import com.njtoyo.taxi.admin.service.business.driver.DriverRewardService;
import com.taxi.entity.common.RestResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class DriverRewardServiceImpl implements DriverRewardService {
    @Autowired
    private DriverRewardMapper driverRewardMapper;
    @Autowired
    private DriverCustomMessageMapper driverCustomMessageMapper;
    @Autowired
    private DriverRewardLogMapper driverRewardLogMapper;
    @Autowired
    private DriverFlowLogMapper driverFlowLogMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestResult add(DriverRewardAddOrUpdateWrapper wrapper) {
        List<Long> driverIds = new ArrayList<>();
        for (Long driverId : wrapper.getDriverIds()) {
            DriverReward driverReward = new DriverReward()
                    .setDriverId(driverId)
                    .setAmount(wrapper.getRewardAmount())
                    .setCategory(DriverRewardCategory.redPacket)
                    .setArrivalTime(DateUtil.date())
                    .setIsVerified(true)
                    .setThrough("other")
                    .setMarketingId(2L);
            driverRewardMapper.insert(driverReward);
            driverIds.add(driverReward.getId());

            DriverRewardLog driverRewardLog = new DriverRewardLog()
                    .setAmount(wrapper.getRewardAmount())
                    .setDriverId(driverId);
            driverRewardLogMapper.insert(driverRewardLog);


            DriverCustomMessage driverCustomMessage = new DriverCustomMessage()
                    .setDriverId(driverId)
                    .setContent("您收到了系统发放的奖励" + wrapper.getRewardAmount() + "元")
                    .setIsPlainText(true)
                    .setLinkCategory(DriverCustomMessageLinkCategory.self)
                    .setEventCategory(DriverCustomMessageEventCategory.system);
            driverCustomMessageMapper.insert(driverCustomMessage);


            DriverFlowLog driverFlowLog = new DriverFlowLog().setDriverId(driverId)
                    .setAmount(wrapper.getRewardAmount())
                    .setCategory(DriverFlowLogCategory.ride_red_packet)
                    .setInOut(DriverFlowLogInOut.in);
            driverFlowLogMapper.insert(driverFlowLog);
        }

        return RestResult.success().add("driverRewardId",driverIds);
    }
}
